package org.application.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.application.Entity.Piece;
import org.application.service.MoveValidator;
import java.util.Map;

@Slf4j
public class KnightMoveValidator implements MoveValidator {
    @Override
    public boolean validateMove(Map<String, Piece> pieceMap, Piece selectedPiece, int startRow, int startCol, int targetRow, int targetCol) {
        // 马的规则：L形移动（2格横向 + 1格纵向，或 2格纵向 + 1格横向）
        int rowDiff = Math.abs(targetRow - startRow);
        int colDiff = Math.abs(targetCol - startCol);

        if ((rowDiff == 2 && colDiff == 1) || (rowDiff == 1 && colDiff == 2)) {
            // 检查别马脚规则
            int midRow = startRow, midCol = startCol;

            // 这是横向马走
            if (rowDiff == 2) {
                // 横向的中间值(这里我们需要考虑的是移动棋子到目标棋子的横坐标中间值（可能是当前横坐标的+1,也可能是-1位置） + 移动棋子的当前竖坐标)
                midRow = startRow + (targetRow - startRow) / 2;
            } else if (colDiff == 2) { // 竖向马走
                midCol = startCol + (targetCol - startCol) / 2;
            }

            String midPosition = midRow + "," + midCol;
            if (pieceMap.containsKey(midPosition) && pieceMap.get(midPosition) != null) {
                log.warn("马的路径被阻挡（别马脚）：中间点 ({}, {}) 有棋子", midRow, midCol);
                return false;
            }

            return true;
        }

        log.warn("马只能按 'L' 形移动");
        return false;
    }
}
